// pages/pay/pay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    orderInfo: {},
    payType: '2',
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  onLoad(options) {
    this.loadOrderDetail();
    
  },

  loadOrderDetails: function() {
    var token = wx.getStorageSync('token');
    var that = this;
    const id = this.data.orderId
    console.log(id);
    this.setData({ loading: true });
    
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/order/detail',
      method: 'GET',
      header: {
        'x-byesame-token': token
      },
      data: {
        orderId: id
      },
      success: function(res) {
        console.log('pay',res);
        that.setData({ loading: false });
        if (res.statusCode === 200) {
          that.setData({
            orderInfo: res.data.data.orderInfo
          });
        } else {
          wx.showToast({
            title: res.data.msg || '获取订单详情失败',
            icon: 'none'
          });
        }
      },
      fail: function(err) {
        that.setData({ loading: false });
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },
  
  loadOrderDetail: function () {
    var token = wx.getStorageSync('token');
  
    this.setData({
      loading: true
    });
  
    // 在函数开头保存this引用
    const that = this;
  
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/order/submit',
      method: 'POST',
      header: {
        'x-byesame-token': token
      },
      data: {
        addressId: wx.getStorageSync('addressId') || 0,
        cartId: wx.getStorageSync('cartId') || 0,
        couponId: wx.getStorageSync('couponId') || 0,
        grouponRulesId: wx.getStorageSync('grouponRulesId') || 0,
        grouponLinkId: wx.getStorageSync('grouponLinkId') || 0,
        message: wx.getStorageSync('message') || '',
        userCouponId: wx.getStorageSync('userCouponId') || 0
      },
      success: function (res) {
        console.log('订单提交响应:', res);
        
        if (res.statusCode === 200 && res.data && res.data.data) {
          // 使用that而不是this
          that.setData({
            orderId: res.data.data.orderId
          });
          
          // 修正：使用that调用loadOrderDetails
          that.loadOrderDetails();
          
          console.log('订单ID:', that.data.orderId);
          
        } else {
          wx.showToast({
            title: res.data.msg || '提交订单失败',
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function (err) {
        console.error('请求失败:', err);
        that.setData({
          loading: false
        });
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none',
          duration: 2000
        });
      },
      complete: function () {
        // 请求完成后的处理
        that.setData({
          loading: false
        });
      }
    });
  },
  confirmPay: function () {
    var that = this;
    var payTypeText = this.getPayTypeText(this.data.payType);

    wx.showModal({
      title: '确认支付',
      content: '你选择了' + payTypeText + '支付',
      confirmText: '确认',
      cancelText: '取消',
      success: function (res) {
        if (res.confirm) {
          that.payOrder();
        }
      }
    });
  },
  selectPayType: function (e) {
    var payType = e.currentTarget.dataset.type;
    console.log('选择的支付方式:', payType);

    this.setData({
      payType: payType
    });
  },
  getPayTypeText: function (payType) {
    var payTypes = {
      '1': '支付宝',
      '2': '微信支付'
    };
    return payTypes[payType] || '未知支付方式';
  },
  payOrder: function () {
    var token = wx.getStorageSync('token');
    var that = this;

    wx.showLoading({
      title: '支付中...',
    });

    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/order/h5pay',
      method: 'POST',
      header: {
        'x-byesame-token': token,
        'content-type': 'application/json'
      },
      data: {
        orderId: that.data.orderId
      },
      success: function (res) {
        wx.hideLoading();
        if (res.statusCode === 200) {
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          });

          // 支付成功后跳转回订单页面
          
        } else {
          wx.showToast({
            title: res.data.msg || '支付失败',
            icon: 'none'
          });
        }
      },
      fail: function (err) {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})